//661. 图片平滑器
//思路：1.计算当前位置有多少个存在的元素，将这几个元素累加起来
//2.用和除以元素个数，放在开辟的二维数组的相对应位置上

#include <stdlib.h>
#include <string.h>
int** imageSmoother(int** img, int imgSize, int* imgColSize, int* returnSize, int** returnColumnSizes){
    int m = imgSize, n = imgColSize[0];
    int** ret = (int**)malloc(sizeof(int*) * m);
    *returnColumnSizes = (int*)malloc(sizeof(int) * m);
    for(int i = 0; i < m; i++)
    {
        ret[i] = (int*)malloc(sizeof(int) * n);
        memset(ret[i], 0, sizeof(int)*n);
        (*returnColumnSizes)[i] = n;
    }
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            int num = 0, sum = 0;
            for(int x = i - 1; x <= i + 1; x++)
            {
                for(int y = j - 1; y <= j + 1; y++)
                {
                    if(x >= 0 && x < m && y >= 0 && y < n)
                    {
                        num++;
                        sum += img[x][y];
                    }
                }
            }
            ret[i][j] = sum / num;
        }
    }
    *returnSize = m;
    return ret;
}